<?php

namespace brand\page;

use brand\build\Columns;
use brand\logic\BrandHelper;
use brand\logic\Config;
use brand\tools\Functions;
use surface\Factory;
use function url;

class Form extends Page
{
    public static $action = 'add';
    public static $SurfaceForm;
    public static $searchRaw = [];


    public static function init($curdName)
    {
        self::$curdName = $curdName;
        self::$SurfaceForm = Factory::form();
        self::$searchRaw = request()->get();
    }

    /**
     * 设置异步地址
     * @param string $url
     */
    public static function setAsyncUrl(string $url = '')
    {
        self::$SurfaceForm->options(
            [
                'async' => [
                    'url' => $url,
                ]
            ]
        );
    }
    /**
     * 构建表单数据
     * @param $columns
     * @param $raw
     * @param $is_search
     * @return array
     */
    public static function buildForm( $columns ,$raw,$is_search = false)
    {

        $form = self::$SurfaceForm;
        $columns_arr = [];
        foreach ($columns as $key => $column) {

            if (is_object($column) && strstr(get_class($column),'Builder')){
                $column = $column->build();
            }
            if (is_object($column)){
                $columns_arr[] = $column;
                continue;
            }

            //表单是否显示
            if (!self::columnsFormIsShow($column,$is_search)) continue;

            if ($is_search && in_array($key,['delete_time','update_time'])) continue;

            $column['form'] = $column['form'] ?? [];


            //当前表单规则
            $this_form = $column['form'];
            if (isset($this_form[self::$action]) && $this_form[self::$action]){
                foreach ($this_form[self::$action] as $action_key => $item) {
                    $this_form[$action_key] = $item;
                }
            }
            //默认输入框
            $method = $this_form['method'] ?? 'input';
            //标题
            $title = $column['title'] ?? $key;
            //默认值
            $value = $raw[$key] ?? ($this_form['default'] ?? "");
            //方法类
            $ClassName = Config::BRAND_NAMESPACE . Functions::toCamelCase($method);
            //类是否存在
            if (class_exists($ClassName)){

                if (is_object($raw)) $raw = $raw->toArray();
                if (!$raw) $raw = [];
                $ClassObject = new $ClassName($column,$raw);
                $ClassObject->field = $key;
                $ClassObject->is_search = $is_search;
                $ClassObject->raws = $raw;
                $ClassObject->buildColumns();
                //构建表单
                $Column = $ClassObject->Form();

            }else{
                //构建默认表单
                $Column = $form->{$method}($key,$title,$value);
            }
            //插入构建的表单数据
            $columns_arr[] = $Column;
        }

        //返回构建的表单数组
        return $columns_arr;
    }

    public static function buildColumns($columns_arr)
    {
        return self::$SurfaceForm->columns($columns_arr);
    }

    public static function buildView()
    {
        return self::$SurfaceForm->view();
    }

    /**
     * 搜索组件
     */
    public static function builderSearch($columns,$raws = [])
    {

        $columns_arr = self::buildForm($columns,$raws,true);
        self::$SurfaceForm->columns($columns_arr);
        return self::$SurfaceForm;

    }


    /**
     * 表单是否显示
     * @param $column
     * @param int $is_search
     * @return bool
     */
    public static function columnsFormIsShow($column , $is_search = 0)
    {
        if (!$is_search){
            //输入信息不存在
            if (!isset($column['form'])) return false;
            //不显示form
            if (isset($column['form']['status']) && !$column['form']['status']) return false;
            //不是搜索 过滤关联表
            if (isset($column['form']['is_relevancy']) && $column['form']['is_relevancy']) return false;
            //判断自动写入时间
            if (isset($column['form']['create_time']) || isset($column['form']['update_time']) || isset($column['form']['delete_time'])) return false;
            //判断特殊字符
            if (isset($column['special_type']) && isset(Config::SPECIAL_TYPE[$column['special_type']])) return false;
        }else{
            if (!isset($column['search']) || !$column['search']) return false;
        }

        return true;
    }


    public static function createForm($column)
    {
        $form = new self();
        Columns::setConfig($column);
        return $form;
    }

    public function setDataUrl($url)
    {
        $this::setAsyncUrl ($url);
        return $this;
    }

    public function formView($raw)
    {
        //构造Form表单
        $columns_arr = $this::buildForm(Columns::getConfig(),$raw);

        $this::buildColumns($columns_arr);

        return $this::buildView();
    }

}